package lib.bone.admin.controller.dict;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.access.module.dict.dto.NepuBookDictAddDTO;
import lib.bone.access.module.dict.dto.NepuBookDictQueryDTO;
import lib.bone.access.module.dict.dto.NepuBookDictUpdateDTO;
import lib.bone.access.module.dict.entity.NepuBookDict;
import lib.bone.access.module.dict.DictServiceCtl;
import lib.bone.admin.anno.WebLog;
import lib.bone.admin.constant.MethodTypeConst;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author: kindear
 */
@Slf4j
@RestController
@RequestMapping("nepu/book")
@Api(tags = "著作教材词典")
public class NepuBookDictController {


    @Autowired
    DictServiceCtl dictServiceCtl;

    @GetMapping(value = "dicts")
    @ApiOperation(value = "查询全部",notes = "")
    public ResponseDTO getAll(NepuBookDictQueryDTO nepuBookDictQueryDTO){

        Sort sort;
        if(nepuBookDictQueryDTO.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, nepuBookDictQueryDTO.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, nepuBookDictQueryDTO.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(nepuBookDictQueryDTO.getPageNum() - 1 , nepuBookDictQueryDTO.getPageSize(), sort);

        NepuBookDict nepuBookDict = new NepuBookDict();
        BeanUtil.copyProperties(nepuBookDictQueryDTO,nepuBookDict, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<NepuBookDict> example = Example.of(nepuBookDict);

        //return nepuPaperDictService.getAll(pageable,example);
        return ResponseDTO.succData(dictServiceCtl.getAll(pageable,example, ResearchTypeConst.BOOK));
      //  return nepuBookDictService.getAll(pageable,example);
    }

    @WebLog(module = "nepu-access")
    @PostMapping(value = "dicts")
    @ApiOperation(value = "新建记录",notes = "")
    public ResponseDTO saveOne(@RequestBody @Validated NepuBookDictAddDTO nepuBookDictAddDTO){
        NepuBookDict nepuBookDict = new NepuBookDict();
        BeanUtil.copyProperties(nepuBookDictAddDTO,nepuBookDict,CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        return dictServiceCtl.saveOne(nepuBookDict);
//        return nepuAwardDictService.saveOne(nepuAwardDictAddDTO);
    }

    @GetMapping("dicts/{id}")
    @ApiOperation(value = "查询单条记录",notes = "传入ID")
    public ResponseDTO getOne(@PathVariable("id") String id){
        Optional<Object> nepuBookDictOp = dictServiceCtl.getById(id,ResearchTypeConst.BOOK);
        return nepuBookDictOp.<ResponseDTO>map(ResponseDTO::succData).orElseGet(() -> ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND, "ID:[" + id + "]对应的数据不存在"));
    }

    @WebLog(module = "nepu-access",operate = MethodTypeConst.DELETE)
    @DeleteMapping("dicts/{id}")
    @ApiOperation(value = "删除单条记录",notes = "传入ID")
    public ResponseDTO deleteOne(@PathVariable("id") String id){
        return dictServiceCtl.deleteById(id,ResearchTypeConst.BOOK);
    }

    @WebLog(module = "nepu-access",operate = MethodTypeConst.UPDATE)
    @PutMapping("dicts")
    @ApiOperation(value = "更新单条记录",notes = "")
    public ResponseDTO updateOne(@RequestBody @Validated NepuBookDictUpdateDTO nepuBookDictUpdateDTO){
        Optional<Object> dictOp = dictServiceCtl.getById(nepuBookDictUpdateDTO.getId(),ResearchTypeConst.BOOK);
        if (!dictOp.isPresent()){
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
        }
        NepuBookDict dict = (NepuBookDict) dictOp.get();
        BeanUtil.copyProperties(nepuBookDictUpdateDTO,dict,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return dictServiceCtl.updateOne(dict,ResearchTypeConst.BOOK);
    }
}
